const { sendNotification } = require('../mqtt/mqttClient');

class NotificationService {
  // 发送新事件通知
  static sendNewEventNotification(event) {
    const recipients = this._getRecipients(event.severity);
    
    recipients.forEach(userId => {
      sendNotification(userId, {
        type: 'new_event',
        eventId: event.id,
        title: `新事件: ${event.title}`,
        message: event.description || '请及时处理',
        severity: event.severity,
        timestamp: new Date().toISOString(),
        status: 'pending'
      });
    });
  }
  
  // 发送事件更新通知
  static sendEventUpdate(event) {
    const recipients = this._getRecipients(event.severity);
    
    let title, message;
    
    switch(event.status) {
      case 'acknowledged':
        title = `事件已确认: ${event.title}`;
        message = `已被工作人员确认`;
        break;
      case 'resolved':
        title = `事件已解决: ${event.title}`;
        message = `问题已解决`;
        break;
      default:
        title = `事件更新: ${event.title}`;
        message = `状态变更为: ${event.status.toUpperCase()}`;
    }
    
    recipients.forEach(userId => {
      sendNotification(userId, {
        type: 'event_update',
        eventId: event.id,
        title,
        message,
        severity: event.severity,
        timestamp: new Date().toISOString(),
        status: event.status
      });
    });
  }
  
  // 根据严重性确定接收者
  static _getRecipients(severity) {
    // 实际项目中从数据库获取用户订阅关系
    switch(severity) {
      case 'critical': return [101, 102, 103]; // 主管+技术团队
      case 'high':    return [101, 102];      // 主管
      default:       return [101];            // 值班人员
    }
  }
}

module.exports = NotificationService;